package com.lcw.lostandfound.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lcw.lostandfound.mapper.GoodsMapper;
import com.lcw.lostandfound.pojo.FindImg;
import com.lcw.lostandfound.pojo.Goods;
import com.lcw.lostandfound.service.GoodsService;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private ImgServiceImpl imgService;

    @Override
    public int delete(Integer id) {
        return goodsMapper.delete(id);
    }

    @Override
    public int insert(Goods goods) {
        return goodsMapper.insert(goods);
    }

    @Override
    public int update(Goods goods) {
        return goodsMapper.update(goods);
    }

    @Override
    public List<Goods> findAllGoods() {
        return goodsMapper.findAllGoods();
    }

    @Override
    public Goods findById(Integer id) {
        return goodsMapper.findById(id);
    }

    @Override
    public PageInfo<Goods> findByLike(Integer pageNum,Integer pageSize,String search) {
        PageHelper.startPage(pageNum,pageSize);
        List<Goods> goods = goodsMapper.findByLike(search);
        System.out.println(goods);
        return new PageInfo<>(goods);
    }


    @Override
    public Integer release(Goods goods) {
        Integer type= goods.getType();
        if(type==1){
            goods.setCategory("15");
        }
        if(type==2){
            goods.setCategory("");
        }
        Document document = Jsoup.parse(goods.getDetail());
        Elements imgEles=document.getElementsByTag("img");
        for (Element ele:imgEles){
            ele.attr("style","max-width:600px");
        }
        goods.setDetail(document.body().html());
        goodsMapper.insert(goods);
        this.handleData(goods,document);
        return goods.getId();
    }
    private void handleData(Goods goods,Document document){
        Element imgEle=document.getElementsByTag("img").first();
        while (true){
            if(imgEle!=null){
                if(imgEle.attr("src").contains("/editor/plugins/emoticons/images/")||imgEle.attr("src").contains("http://api.map.baidu.com")){
                    imgEle.remove();
                    imgEle=document.getElementsByTag("img").first();
                }else {
                    imgService.saveImg(new FindImg(goods.getTitle(),imgEle.attr("src"),goods.getId(),goods.getUserId()));
                    System.out.println("图片保存完成。。。");
                    break;
                }
            }else {
                break;
            }
        }
    }

    @Override
    public int updateViews(Integer id) {
        return goodsMapper.updateViews(id);
    }

    @Override
    public List<Goods> findByUserId(Integer userId) {
        return goodsMapper.findByUserId(userId);
    }


    //首页寻物启事列表
    @Override
    public PageInfo<Goods> listWallsFindMsg() {
        PageHelper.startPage(1,20);
        List<Goods> allGoods = goodsMapper.listFindMsg(0);
        return new PageInfo<>(allGoods);
    }
    //首页招领启事列表
    @Override
    public PageInfo<Goods> listWallsLostMsg() {
        PageHelper.startPage(1,20);
        List<Goods> allGoods = goodsMapper.listFindMsg(3);
        return new PageInfo<>(allGoods);
    }

    @Override
    public PageInfo<Goods> listGoods(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Goods> allGoods = goodsMapper.findAllGoods();
        return new PageInfo<>(allGoods);
    }

    //启事列表
    @Override
    public PageInfo<Goods> listFindMsg(Integer pageNum, Integer pageSize, Integer type) {
        PageHelper.startPage(pageNum,pageSize);
        List<Goods> goods = goodsMapper.listFindMsg(type);
        return new PageInfo<>(goods);
    }
}
